<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script>
      /**
       * @param {string} s
       * @return {string}
       */
      var longestPalindrome = function (s) {
        let longest = "";
        for (let i = 0; i < s.length; i++) {
          let long = ""
          let len = s.length - 1 - i < i ? s.length - 1 - i : i;
          for (let j = 1; j <= len; j++) {
            if(s[i-j] === s[i+j]) {
              long = s.slice(i-j, i + j + 1)
            } else {
              break;
            }
          }
          let evenLong = ""
          let evenLen = s.length - 1 - i < i ? s.length - i : i + 1;
          for (let j = 1; j <= evenLen; j++) {
            if(s[i+1-j] === s[i+j]) {
              evenLong = s.slice(i + 1 - j, i + 1 + j)
            } else {
              break;
            }
          }
          if(long.length>longest.length) {
            longest = long
          }
          if(evenLong.length>longest.length) {
            longest = evenLong
          }
        }
        if(!longest&&s.length) {
          longest = s[0]
        }
        return longest;
      };
      // let ss = "babad";
      // let ss = "bb";
      // let ss = "cbbd";
      let ss = "bacabab";
      // let ss = "ccc";
      let s = longestPalindrome(ss);
      console.log(s);
    </script>
  </body>
</html>
